#include "head.h"
#include <dirent.h>

int switch_dir(int net_fd,char** arg){

    char* path = arg[0];

    DIR *dir = opendir(path);
    int sig;
    if(dir == NULL){
        sig = ERROR;
        send(net_fd,&sig,sizeof(int),MSG_WAITALL);
        return 0;
    }else{
        sig = CD;
        send(net_fd,&sig,sizeof(int),MSG_WAITALL);
    }

    int path_len = strlen(path);
    send(net_fd,&path_len,sizeof(int),MSG_WAITALL);
    send(net_fd,path,path_len,MSG_WAITALL);

    closedir(dir);
    return 0;
}

int view_dir(int net_fd,char** arg){

    char* path;
    if(arg[0] != NULL){
        path = arg[0];
    }else{
        path = ".";
    }

    DIR *dir = opendir(path);
    int sig;
    if(dir==NULL){
        sig = ERROR;
        send(net_fd,&sig,sizeof(int),MSG_WAITALL);
    }else{
        sig = LS;
        send(net_fd,&sig,sizeof(int),MSG_WAITALL);
    }
    
    struct dirent* pdir = readdir(dir);

    int count = 0;
    while(1){
        if(pdir == NULL){
            int end = 99;
            send(net_fd,&end,sizeof(int),0);
            printf("\n");
            break;
        }else if(strcmp(pdir->d_name,".")==0||strcmp(pdir->d_name,"..")==0){
            pdir = readdir(dir);
            continue;
        }
        printf("%-16s",pdir->d_name);
        int name_len = strlen(pdir->d_name);
        send(net_fd,&name_len,sizeof(int),0);
        send(net_fd,pdir->d_name,name_len,0);
        count++;
        if(count%6 == 0){
            printf("\n");
        }
        pdir = readdir(dir);
    }


    closedir(dir);
    return 0;
}


int upload_file(int net_fd,char** arg_list){

    char* file_name;
    if(arg_list[1] != NULL){
        printf("upload : error argument");
        return 0;
    }else{
        file_name = arg_list[0];
    }

    int file_fd = open(file_name,O_CREAT|O_RDWR|O_TRUNC,0666);
    if(file_fd < 1){
        
    }
    off_t file_size = 0;
    int ret_recv = recv(net_fd,&file_size,sizeof(off_t),MSG_WAITALL);
    ERROR_CHECK(ret_recv,-1,"recv");

    char* buf;
    ret_recv = recv(net_fd,buf,file_size,MSG_WAITALL);
    ERROR_CHECK(ret_recv,-1,"recv");

    write(file_fd,buf,file_size);

    close(file_fd);

    return 0;

}


int craeate_dir(int net_fd,char** arg_list){

    

}



void start_comm(int net_fd,char* command){                                                                                        

  char* comm = strtok(command," ");

  char* token = comm;

  int token_num = 0;
  char** arg_list;
  while(token != NULL){
      token = strtok(NULL," ");
      token_num++;
      arg_list = calloc(token_num,sizeof(char*));
      arg_list[token_num] = token;
  }   

  switch(*comm){

  case 'c':
     switch_dir(net_fd,arg_list);
     break;
  case 'l':
     view_dir(net_fd,arg_list);
     break;
  case 'p':
      switch(*(comm + 1)){
      case 'u':
          upload_file(net_fd,arg_list);
          break;
      case 'w':
          //check_path(arg_list);
         }   
  case 'g':
      //download_file(arg_list);
  case 'r':
      //delete_file(arg_list);
     }   

  return;

}













